//
//  GMyActivityVC.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/22.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "GMyActivityVC.h"
#import "IGMyFocusActivityApi.h"
#import "MyGActivityCell.h"
#import "HotMeActivityCell.h"
#import "NoneFocusCell.h"
#import "IGAttentionActivityApi.h"
#import "IGExhibitionVC.h"
#import "IGCompetitionVC.h"

@interface GMyActivityVC ()
{
    NSString *uid;
    NSString *pageNum;//页数，从1开始
}
@property (nonatomic, strong) IGMyFocusActivityApi *myActivityApi;
@property (nonatomic, strong) IGAttentionActivityApi *attentApi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *recDataArr;
@end

@implementation GMyActivityVC
#pragma mark - 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray *)recDataArr{
    if (!_recDataArr) {
        _recDataArr=[NSMutableArray array];
    }
    return _recDataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor mainBackgroundColor];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyGActivityCell" bundle:nil] forCellReuseIdentifier:@"MyGActivityCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NoneFocusCell" bundle:nil] forCellReuseIdentifier:@"NoneFocusCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HotMeActivityCell" bundle:nil] forCellReuseIdentifier:@"HotMeActivityCell"];
        self.myActivityApi=[IGMyFocusActivityApi new];
        [self firstPage];
}
-(void)firstPage{
    pageNum= @"1";
    uid=[IGUser getUid];
    [self requestData];
}
-(void)requestData{
    self.myActivityApi.pageNum=pageNum;
    self.myActivityApi.uid=uid;
    @KWeakObj(self)
    [self.myActivityApi startWithDecodeClass:[IGMyFocusActivityResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyFocusActivityResp *resp=responseObject;
            IGMyFocusActivityModel *model=resp.data;
            if (model.focusActivityList&&model.focusActivityList.count>0) {
                [selfWeak setupFooter];
            }
            if ([pageNum isEqualToString:@"1"])
            {
                _dataArr = [NSMutableArray arrayWithArray:model.focusActivityList];
                _recDataArr=[NSMutableArray arrayWithArray:model.recommendActivityList];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.focusActivityList];
                [_recDataArr removeAllObjects];
                [_recDataArr addObjectsFromArray:model.recommendActivityList];
            }
            [selfWeak.tableView reloadData];
        }
    }];
}
- (void)scrollToTOP
{
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        pageNum = [NSString stringWithFormat:@"%d", [pageNum intValue] + 1];
        [selfWeak requestData];
    }];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataArr.count!=0) {
        return 1;
    }
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return _dataArr.count?_dataArr.count:0;
    }else if (section==1){
        return 1;
    }
    if (section==2) {
        return _recDataArr.count?_recDataArr.count:0;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        MyGActivityCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyGActivityCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleDefault;
        cell.data=_dataArr[indexPath.row];
        return cell;
    }
    if (indexPath.section==1){
        NoneFocusCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NoneFocusCell" forIndexPath:indexPath];
        cell.focusType=@"2";
        return cell;
    }
    if (indexPath.section==2) {
        HotMeActivityCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HotMeActivityCell" forIndexPath:indexPath];
        cell.data=_recDataArr[indexPath.row];
        [cell followOwerAction:^(MyFocusMemberModel *info) {
            self.attentApi = [[IGAttentionActivityApi alloc]init];
            _attentApi.operatorType = [info.attentionState boolValue]?@"0":@"1";
            _attentApi.activityId = info.aid;
            [self.attentApi startWithDecodeClass:[IGAttentionActivityResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
                    
                } else {
                    if ([info.attentionState boolValue]) {
                        [self presentMessageTips:NSLocalizedString(@"Follow Failed", nil)];
                    } else {
                        [self presentMessageTips:NSLocalizedString(@"Cancel Failed", nil)];
                    }
                }
            }];
        }];
        [cell setNeedsLayout];
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyFocusMemberModel *model=_dataArr[indexPath.row];
    if (indexPath.section==0) {
            if ([model.type isEqualToString:@"1"]) {        ///>展览
                IGExhibitionVC *exhibitionVC = [[IGExhibitionVC alloc] init];
                exhibitionVC.activityId = model.aid;
                [UIViewController ig_pushViewController:exhibitionVC animated:YES];
            } else if ([model.type isEqualToString:@"2"]) { ///>比赛
                IGCompetitionVC *competitionVC = [[IGCompetitionVC alloc] init];
                competitionVC.activityId = model.aid;
                [UIViewController ig_pushViewController:competitionVC animated:YES];
            }
         [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 101;
    }
    if (indexPath.section==1) {
        return 120;
    }
    if (indexPath.section==2) {
        return 100;
    }
    return 71;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 5;
    }
    if (section==1) {
        return 5;
    }
    if (section==2) {
        return 51;
    }
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==1) {
        return 5;
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==2) {
        UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 51)];
        backView.backgroundColor=[UIColor mainBackgroundColor];
        UIView *frontView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 50)];
        frontView.backgroundColor=[UIColor whiteColor];
        UILabel *hot=[[UILabel alloc]initWithFrame:CGRectZero];
        hot.text=@"热门艺术馆推荐";
        hot.font=[UIFont font16];
        hot.textColor=[UIColor c4Color];
        [hot sizeToFit];
        hot.x=KSCREENWIDTH/2-hot.width/2;
        hot.centerY=frontView.centerY;
        [frontView addSubview:hot];
        UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame=CGRectMake(0, 0, 50, 30);
        [changeBtn setTitle:@"换一批" forState:UIControlStateNormal];
        [changeBtn setTitle:@"换一批" forState:UIControlStateSelected];
        [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateNormal];
        [changeBtn setImage:[UIImage imageNamed:@"gexchange"] forState:UIControlStateSelected];
        [changeBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateNormal];
        [changeBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
        [changeBtn sizeToFit];
        changeBtn.centerY=frontView.centerY;
        changeBtn.right=frontView.right-12;
        
        [changeBtn addTarget:self action:@selector(changeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [frontView addSubview:changeBtn];
        [backView addSubview:frontView];
        return backView;
    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section==1) {
        UIView *footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 5)];
        return footer;
    }
    return nil;
}

-(void)changeBtnClick{
    pageNum = [NSString stringWithFormat:@"%ld", [pageNum integerValue] + 1];
    [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
